From 1a985a5b732c9e69158f3d45c01f4e9a974078bb Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sat, 16 Aug 2014 12:27:31 -0700 Subject: [PATCH] Ensure TARGET is always present for build commands --- src/cargo/ops/cargo_rustc/context.rs | 33 +++++++++++++++++++++++----- src/cargo/ops/cargo_rustc/mod.rs | 2 +- tests/test_cargo_bench.rs | 8 +++---- 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/cargo/ops/cargo_rustc/context.rs b/src/cargo/ops/cargo_rustc/context.rs index 87934fa64..96186d535 100644 --- a/src/cargo/ops/cargo_rustc/context.rs +++ b/src/cargo/ops/cargo_rustc/context.rs @@ -4,7 +4,7 @@ use semver::Version; use core::{SourceMap, Package, PackageId, PackageSet, Resolve, Target}; use util; -use util::{CargoResult, ChainError, internal, Config, profile}; +use util::{CargoResult, ChainError, internal, Config, profile, Require}; use super::{Kind, KindPlugin, KindTarget, Compilation}; use super::layout::{Layout, LayoutProxy}; @@ -27,6 +27,7 @@ pub struct Context<'a, 'b> { env: &'a str, host: Layout, target: Option, + target_triple: String, host_dylib: (String, String), package_set: &'a PackageSet, target_dylib: (String, String), @@ -47,8 +48,10 @@ impl<'a, 'b> Context<'a, 'b> { let (dylib, _) = try!(Context::filename_parts(None)); dylib }; + let (rustc_version, target_triple) = try!(Context::rustc_version()); Ok(Context { - rustc_version: try!(Context::rustc_version()), + rustc_version: rustc_version, + target_triple: target_triple, env: env, host: host, target: target, @@ -65,11 +68,26 @@ impl<'a, 'b> Context<'a, 'b> { }) } - /// Run `rustc` to figure out what its current version string is - fn rustc_version() -> CargoResult { + /// Run `rustc` to figure out what its current version string is. + /// + /// The second element of the tuple returned is the target triple that rustc + /// is a host for. + fn rustc_version() -> CargoResult<(String, String)> { let output = try!(util::process("rustc").arg("-v").arg("verbose") .exec_with_output()); - Ok(String::from_utf8(output.output).unwrap()) + let output = try!(String::from_utf8(output.output).map_err(|_| { + internal("rustc -v didn't return utf8 output") + })); + let triple = { + let triple = output.as_slice().lines().filter(|l| { + l.starts_with("host: ") + }).map(|l| l.slice_from(6)).next(); + let triple = try!(triple.require(|| { + internal("rustc -v didn't have a line for `host:`") + })); + triple.to_string() + }; + Ok((output, triple)) } /// Run `rustc` to discover the dylib prefix/suffix for the target @@ -205,6 +223,11 @@ impl<'a, 'b> Context<'a, 'b> { (pair.ref0().as_slice(), pair.ref1().as_slice()) } + /// Return the target triple which this context is targeting. + pub fn target_triple(&self) -> &str { + self.target_triple.as_slice() + } + /// Return the exact filename of the target. pub fn target_filenames(&self, target: &Target) -> Vec { let stem = target.file_stem(); diff --git a/src/cargo/ops/cargo_rustc/mod.rs b/src/cargo/ops/cargo_rustc/mod.rs index 40d443e3c..ca0f6e843 100644 --- a/src/cargo/ops/cargo_rustc/mod.rs +++ b/src/cargo/ops/cargo_rustc/mod.rs @@ -167,7 +167,7 @@ fn compile_custom(pkg: &Package, cmd: &str, let mut p = process(cmd.next().unwrap(), pkg, cx) .env("OUT_DIR", Some(&output)) .env("DEPS_DIR", Some(&output)) - .env("TARGET", cx.config.target()); + .env("TARGET", Some(cx.target_triple())); for arg in cmd { p = p.arg(arg); } diff --git a/tests/test_cargo_bench.rs b/tests/test_cargo_bench.rs index 9c2e85ca1..cae06099c 100644 --- a/tests/test_cargo_bench.rs +++ b/tests/test_cargo_bench.rs @@ -482,7 +482,7 @@ test!(lib_bin_same_name { version = "0.0.1" authors = [] - [[lib]] + [lib] name = "foo" [[bin]] name = "foo" @@ -591,7 +591,7 @@ test!(lib_with_standard_name2 { version = "0.0.1" authors = [] - [[lib]] + [lib] name = "syntax" bench = false doctest = false @@ -666,7 +666,7 @@ test!(bench_dylib { version = "0.0.1" authors = [] - [[lib]] + [lib] name = "foo" crate_type = ["dylib"] @@ -695,7 +695,7 @@ test!(bench_dylib { version = "0.0.1" authors = [] - [[lib]] + [lib] name = "bar" crate_type = ["dylib"] "#) -- 2.30.2